<?php

  $hide_footer = -1;
  include('header.php');
  include('show_smilies.php');   include('functions.php');
  if ($bad_word_filter == 1) {
      include 'word_filter.php';
  }
  if (mysql_real_escape_string($_GET['t'] != '')) {
      if (!isset($_SESSION['signed_in']) and ($topics_for_guests == '0' or $guest_access == "0")) {
          header('location: signin.php');
      } else {
	  if(!is_numeric($_GET['t'])) {
  header('location: index.php');
  }
          //First lets load some JAVA script for post reporting (only if user is logged and report enabled).
          if ($post_report == 1 and isset($_SESSION['signed_in'])) {
              echo " <script>
function confirmReport(delUrl) {
  if (confirm('$l_report_confirm')) {
    document.location = delUrl;
  }
}
</script>";
          }
		  $group = "";
		  $icon = "";
		  $hide_post = 0;
		  $current_page = 1;
		  $locked = "";
		  $sticky = "";
		  $approve = "";
		  $notifym = "";
		  $unnotify ="";
		  $report ="";
		  if (isset($_GET['locked'])) {$locked = mysql_real_escape_string($_GET['locked']);}
		   if (isset($_GET['sticky'])) {$sticky = mysql_real_escape_string($_GET['sticky']);}
		   if (isset($_GET['approve'])) {$approve = mysql_real_escape_string($_GET['approve']);}
		   if (isset($_GET['notify'])) {$notifym = mysql_real_escape_string($_GET['notify']);}
		   if (isset($_GET['unnotify'])) {$unnotify = mysql_real_escape_string($_GET['unnotify']);}
		   if (isset($_GET['report'])) {$report = mysql_real_escape_string($_GET['report']);}
          // update LOCKED, STICKY
          if ($locked == '0' and $_SESSION['user_level'] >= 1) {
              $unlock = $db->query("UPDATE " . $table_prefix . "topics SET topic_status = 0 WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . " LIMIT 1"); 
              echo "<div align='center'><b>$l_topic_unlocked</b></div><br />";
          }
          if ($locked == '1' and $_SESSION['user_level'] >= 1) {
              $unlock = $db->query("UPDATE " . $table_prefix . "topics SET topic_status = 1 WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . " LIMIT 1");
              echo "<div align='center'><b>$l_topic_locked</b></div><br />";
          }
          
          if ($sticky == '1' and $_SESSION['user_level'] >= 1) {
              $unlock = $db->query("UPDATE " . $table_prefix . "topics SET topic_sort = -1 WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . " LIMIT 1");
              echo "<hr><br/><div align='center'><b>$l_topic_type_sticky</b></div><br />";
          }
          if ($sticky == '0' and $_SESSION['user_level'] >= 1) {
              $unlock = $db->query("UPDATE " . $table_prefix . "topics SET topic_sort = 0 WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . " LIMIT 1");
              echo "<hr><br/><div align='center'><b>$l_topic_type_normal</b></div><br />";
          }
          // END update LOCKED, STICKY
          
		  //APPROVE POST
		  if ($approve != '' and $_SESSION['user_level'] >= 1) {
              $approve_this_post = $db->query("UPDATE " . $table_prefix . "posts SET post_approve = 0 WHERE post_id = $approve LIMIT 1");
              $approve_this_post = $db->query("UPDATE " . $table_prefix . "topics SET topic_status = 0 WHERE topic_status = 2 AND topic_id = ".mysql_real_escape_string($_GET['t'])." LIMIT 1"); 			  
              echo "<div align='center'><b>$l_post_approved</b></div><br />";
          }
		  
		  if ($disable_email == 0)
		  {
          //NOTIFY
          if ($notifym == mysql_real_escape_string($_GET['t']) and $_SESSION['signed_in'] == true) {
              $check = $db->query("SELECT topic_id FROM " . $table_prefix . "topics_watch WHERE user_id = " . $_SESSION['user_id'] . " AND topic_id = " . mysql_real_escape_string($_GET['t']) . " LIMIT 1");
              if (mysql_num_rows($check) == 0) {
                  $notify = $db->query("INSERT INTO " . $table_prefix . "topics_watch(topic_id,user_id) VALUES ($notifym, " . $_SESSION['user_id'] . ")") or die(mysql_error());
              } else {
                  echo "<div align='center'>$l_topic_watch</div>";
              }
          }
          //UNNOTIFY
          if ($unnotify == mysql_real_escape_string($_GET['t']) and $_SESSION['signed_in'] == true) {
              $check_unn = $db->query("SELECT topic_id FROM " . $table_prefix . "topics_watch WHERE user_id = " . $_SESSION['user_id'] . " AND topic_id = " . mysql_real_escape_string($_GET['t']) . " LIMIT 1");
              if (mysql_num_rows($check_unn) >= 1) {
                  $delete_notify = $db->query("DELETE FROM " . $table_prefix . "topics_watch WHERE user_id=" . $_SESSION['user_id'] . " AND topic_id=" . mysql_real_escape_string($_GET['t']) . " LIMIT 1") or die(mysql_error());
              }
          }
          //END NOTIFY
          }
          //CHECK IF USER READ TOPIC (WHEN NOTIFIED)
          if (isset($_SESSION['signed_in'])) {
              $notify = $db->query("UPDATE " . $table_prefix . "topics_watch SET status = 0 WHERE user_id = " . $_SESSION['user_id'] . " AND topic_id = " . mysql_real_escape_string($_GET['t']) . " LIMIT 1"); 
          }
          //CHECK IF USER READ TOPIC (WHEN NOTIFIED)
          
		  //POST is REPORTED?
		  if ($report != "" AND $_SESSION['signed_in'] == true)
		  {
		  $post_report = mysql_real_escape_string($_GET['report']);
		  $report_uid = mysql_real_escape_string($_SESSION['user_id']);
		  $report_uname = mysql_real_escape_string($_SESSION['user_name']);
		  $report_uip = mysql_real_escape_string($_SESSION['user_ip']);
		  $reportq = $db->query("INSERT INTO ".$table_prefix."post_reports(post_id,user_id,user_name,user_ip) 
		  VALUES($post_report,$report_uid,'$report_uname','$report_uip') ");
		  if ($reportq) {echo "<div align='center'><b>$l_report_success</b></div>";}
		  }
		  //END POST reporting
          $result2 = $db->query("SELECT topic_id, topic_subject, topic_cat, view, reply, topic_status, topic_sort,topic_icon FROM " . $table_prefix . "topics WHERE topic_id =" . mysql_real_escape_string($_GET['t'])." LIMIT 1");
          $qqq += 1;
          $qqq += 1;
          $qqq += 1;
          $qqq += 1;
          if (!$result2) {
              echo 'Topic could not be displayed, please try again later.';
          } else {
              if (mysql_num_rows($result2) == 0) {
                  echo 'Topic doesn&prime;t exist.<br />';
              } else {
                  $row = $db->fetch_array($result2,'assoc');
                  
                  //TOPIC TRACK UPDATE
                  $track = $db->query("SELECT user_id,topic_id,mark_time FROM " . $table_prefix . "topics_track WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . " LIMIT 1");
                  $update_view = 0;
                  if (mysql_num_rows($track) <= 0 and isset($_SESSION['signed_in'])) {
                      $update_view = 1;
                      $track = $db->query("INSERT INTO " . $table_prefix . "topics_track(user_id,topic_id,forum_id,mark_time) VALUES (" . $_SESSION['user_id'] . "," . mysql_real_escape_string($_GET['t']) . ", $row[topic_cat], NOW() + INTERVAL " . $server_time . " MINUTE + INTERVAL " . $time_difference . " HOUR)") or die(mysql_error());
                  }
                  if (!stristr($_SERVER['HTTP_REFERER'], "topic.php?t=" . mysql_real_escape_string($_GET['t']) . "")) {
                      $update_view = 1;
                  }
                  
                  if ($update_view == 1) {
                      $sql5 = $db->query("UPDATE " . $table_prefix . "topics set view=view+1 WHERE topic_id=" . mysql_real_escape_string($_GET['t'])." LIMIT 1"); 
                  }
                  
                  
                  if ($row['topic_id'] != mysql_real_escape_string($_GET['t'])) {
                      echo "An error occured! Please try again later!";
                  } else {
                      echo "<table><tr>";
                      //BREADCRUMBS
                      $bc = $db->query("SELECT cat_name,cat_id, cat_permission FROM " . $table_prefix . "categories WHERE cat_id =" . $row['topic_cat']);
                      $bcrow = $db->fetch_array($bc,'assoc');
                      //AND BOARD PERMISSIONS
                      if ($bcrow['cat_permission'] == 3 and $_SESSION['signed_in'] and $_SESSION['user_level'] >= 1) {
                          $hide_forum = 0;
                      }
                      if ($bcrow['cat_permission'] == 3 and $_SESSION['signed_in'] and $_SESSION['user_level'] < 1) {
                          $hide_forum = 1;
                      }
                      if ($bcrow['cat_permission'] == 3 and $_SESSION['signed_in'] == false) {
                          $hide_forum = 1;
                      }
                      if ($bcrow['cat_permission'] == 2 and $_SESSION['signed_in'] == false) {
                          $hide_forum = 1;
                      }
                      if ($bcrow['cat_permission'] == 2 and $_SESSION['signed_in']) {
                          $hide_forum = 0;
                      }
                      
                      if ($bcrow['cat_permission'] <= 1) {
                          $hide_forum = 0;
                      }
                      
                      // Someone tried to access protected forum...
                      if ($hide_forum == 1) {
                          echo "<hr><br /><div align='center'>$l_error_permission<br /><br /> <a href = 'index.php'>$l_back_to</a></div><br /><br />";
                      }
                      
                      if ($hide_forum != 1) {
                          if ($breadcrumbs == '1') {
                              if (mysql_real_escape_string($_GET['t']) != "") {
                                  $qqq += 1;
                                  $tags = array('{FORUM}', '{SEPARATOR}', '{F_ID}', '{CAT_NAME}', '{SEPARATOR2}', '{T_SUBJECT}');
                                  $data = array('Forum', $l_breadcrumb_separator, $row['topic_cat'], $bcrow['cat_name'], $l_breadcrumb_separator, $row['topic_subject']);
                                  echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/breadcrumbs.html"));
                              }
                          }
                          //END BREADCRUMBS  
                          $pageTitle = $row['topic_subject'];
						  //Prepare query for mods and admins (show unnapproved posts) or NOT for other users
						  $check_approved = "AND post_approve = 0";
						  if ($_SESSION['user_level']>=1)
						  {$check_approved = "";}
						  //ENABLED (this is for debug)
						  $check_approved = "";
                          $result = $db->query("SELECT COUNT(*) FROM " . $table_prefix . "posts WHERE post_topic=" . mysql_real_escape_string($_GET['t']) . " $check_approved");
                          $qqq += 1;
                          $r = mysql_fetch_row($result);
                          $numrows = $r[0];
                          // number of rows to show per page
                          $rowsperpage = $post_per_page;
                          $totalpages = ceil($numrows / $rowsperpage);
                          if (isset($_GET['page']) && is_numeric($_GET['page'])) {
                              $currentpage = (int)$_GET['page'];
                          } else {
                              $currentpage = 1;
                          }
                          if ($currentpage > $totalpages) {
                              $currentpage = $totalpages;
                          }
                          if ($currentpage < 1) {
                              $currentpage = 1;
                          }
                          $offset = ($currentpage - 1) * $rowsperpage;
                          //Lets find all data about posts in this topic and users
                          $result = $db->query("
						  SELECT 
						  " . $table_prefix . "posts.post_topic,
						  " . $table_prefix . "posts.post_id, 
						  " . $table_prefix . "posts.post_content, 
						  " . $table_prefix . "posts.post_date, 
						  " . $table_prefix . "posts.post_by,
						  " . $table_prefix . "posts.post_smilies, 
						  " . $table_prefix . "posts.poster_ip, 
						  " . $table_prefix . "posts.post_edit_by, 
						  " . $table_prefix . "posts.post_edit_date, 
						  " . $table_prefix . "posts.post_approve, 
						  " . $table_prefix . "users.user_id, 
						  " . $table_prefix . "users.user_name, 
						  " . $table_prefix . "users.user_date, 
						  " . $table_prefix . "users.user_posts, 
						  " . $table_prefix . "users.user_signature, 
						  " . $table_prefix . "users.user_level, 
						  " . $table_prefix . "users.is_banned, 
						  " . $table_prefix . "users.user_avatar, 
						  " . $table_prefix . "users.user_gender,
						  " . $table_prefix . "users.user_www 
						  FROM 
						  " . $table_prefix . "posts 
						  LEFT JOIN 
						  " . $table_prefix . "users 
						  ON 
						  " . $table_prefix . 
						  "posts.post_by = " . $table_prefix . "users.user_id 
						  WHERE 
						  " . $table_prefix . "posts.post_topic = " . mysql_real_escape_string($_GET['t']) . " 
						  ORDER BY 
						  post_date LIMIT $offset, $rowsperpage") or trigger_error("SQL", E_USER_ERROR);
                          
                          //Set counter and temp number for permanent links. 
                          //Let post number start from 1 instead od postid. 
                          //Perm link works with both -> #postid and #current post number in topic
                          $counter = 1;
                          $temp_c = 1;
                          if (isset($_GET['page'])){
						  if(!is_numeric($_GET['page'])) {
                          $current_page = 1;
                          }
						  }
                          if ($current_page == "") {
                              $current_page = 1;
                          }
                          //TOP PAGINATION
                          $range = $max_pagination_link;
                          if ($range >= $totalpages) {
                              $range = $totalpages;
                          }
                          if ($current_page >= $totalpages) {
                              $current_page = $totalpages;
                          }
                          
                          echo '<td align="right" class="pagination"> <b>' . $l_page . ' ' . $current_page . ' ' . $l_page_of . ' ' . $totalpages . '</b> [' . $numrows . ' ' . $l_page_posts . ']  &nbsp; &nbsp;';
                          if ($currentpage > 1) {
                              echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=1'><<</a> ";
                              $prevpage = $currentpage - 1;
                              echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$prevpage'><</a> ";
                          }
                          for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
                              if (($x > 0) && ($x <= $totalpages)) {
                                  if ($x == $currentpage) {
                                      echo " [<b>$x</b>] ";
                                  } else {
                                      echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$x'>$x</a> ";
                                  }
                              }
                          }
                          if ($currentpage != $totalpages) {
                              $nextpage = $currentpage + 1;
                              echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$nextpage'>></a> ";
                              echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$totalpages'>>></a> ";
                          }
                          echo '</td></tr></table>';
                          //PAGINATION END
                          if (!isset($_GET['print']) 
						  and $_SESSION['user_level'] >= 0 
						  and isset($_SESSION['signed_in'])) {
                              //REPLY | NEW TOPIC | NOTIFY
                              $check_notify = $db->query("SELECT user_id FROM " . $table_prefix . "topics_watch WHERE topic_id =" . mysql_real_escape_string($_GET['t']) . " AND user_id = " . $_SESSION['user_id'] . " LIMIT 1");
                              if (mysql_num_rows($check_notify) == 0) {
                                  $reply = "<a class='topicOpt' href='quote.php?t=" . mysql_real_escape_string($_GET['t']) . "&page=$page'>$l_reply</a>";
                                  $notify = "<a class='topicOpt' href='javascript:confirmNotify(\"topic.php?t=" . mysql_real_escape_string($_GET['t']) . "&notify=" . mysql_real_escape_string($_GET['t']) . "\")'>$l_notify_me</a>";
                              } else {
                                  if (mysql_num_rows($check_notify) >= 1) {
                                      $reply = "<a class='topicOpt' href='#reply'>$l_reply</a>";
                                      $notify = "<a class='topicOpt' href='topic.php?t=" . mysql_real_escape_string($_GET['t']) . "&unnotify=" . mysql_real_escape_string($_GET['t']) . "'>$l_unnotify_me</a>";
                                  }
                              }
                              if ($_SESSION['signed_in'] == false or $disable_email!=0) {
                                  $notify = "";
                              }
                              if ($row['topic_status'] == 1) {
                                  $reply = "";
                                  $notify = "";
                              }
                              echo " <script>
function confirmNotify(delUrl) {
  if (confirm('$l_notify_confirm')) {
    document.location = delUrl;
  }
}
</script>
<table><tr><td align='left' bgcolor='#fafafa' height='28px'>
&nbsp;&nbsp;
$reply
<a class='topicOpt' href='create_topic.php?f=$row[topic_cat]'>$l_new_topic</a>
$notify
<a class='topicOpt' href='?f=" . $row['topic_cat'] . "&t=" . mysql_real_escape_string($_GET['t']) . "&page=$page&print=print'>$l_print</a>

</td></tr></table>
";
                              //END REPLY | NEW TOPIC | NOTIFY
                          }
                          //PREPARE BAD WORD FILTER BEFORE LOOP
                          if ($bad_word_filter == 1) {
                              $filter = $db->query("SELECT * FROM " . $table_prefix . "word_filter");
                              $word_filter = $db->fetch_array($filter,'assoc');
                          }
						  //FETCH POST DATA:
                          while ($list = $db->fetch_array($result,'assoc')) {
                              $text = $list['post_content'];
							  $user_gender = $list['user_gender'];
							  $gender_image = "";
							  if ($user_gender == 1) 
							  {
							  $gender_image = "<img title='$l_male' align='absmiddle' src='./style/$default_style/img/male.gif'>";
							  };
							  
							  if ($user_gender == 2) 
							  {
							  $gender_image = "<img title='$l_female' align='absmiddle' src='./style/$default_style/img/female.gif'>";
							  };
                              //PERMA LINKS and Current page
                              $temp_numb = $list['post_id'];
                              //Calculate permanent links (postid and current post in this thread)
                              $temp_numb = ($list['post_id'] - $temp_numb + $counter + ($post_per_page * $page) - $post_per_page);
                              if ($page == "") {
                                  $temp_numb = $counter;
                              }
                              if ($page < 1) {
                                  $counter = 1;
                                  $temp_numb = $temp_c;
                              }
                              //echo 'Total Posts: '.(mysql_num_rows($posts_result)).'<br />';
                              //echo 'Page: '. ceil((mysql_num_rows($posts_result)+1)/$post_per_page);
                              if ($page * $post_per_page - $post_per_page > $numrows) {
                                  $temp_numb = $counter;
                              }
                              //END PERMA LINKS
                              
                              if ($list['post_smilies'] == 0 AND $allow_smilies == 1) {
                                  $text = smilies($text, 'img/smilies/');
                              }
                              //BAD WORD FILTER
                              if ($bad_word_filter == 1) {
                                  if ($word_filter['bad_words'] != "") {
                                      $banned_word_list = $word_filter['bad_words'];
                                      $text = wordfilter($banned_word_list, $text);
                                  }
                              }
                              //BAD WORD FILTER
                              $pages = $page;
                              if ($page < 1) {
                                  $pages += 1;
                              }
                              
                              //Nasty way to remove iframe
                              $text = str_replace('%69%66%72%61%6D%65', 'iframe', $text);
							  
                              if ($list['user_level'] == 1) {
                                  $group = "<span style='color:red;'>$l_administrator</span><br />";
                              }
                              if ($list['user_level'] == 0) {
                                  $group = "$l_member<br />";
                              }
                              if ($list['user_level'] < 0) {
                                  $group = "";
                              }
                              
                              $djoined = date($date_author_row, strtotime($list['user_date']));
                              $joined = "$l_joined";
                              $uposts = $list['user_posts'];
                              $unposts = "$l_posts";
                              if ($list['user_level'] < 0) {
                                  $djoined = "";
                                  $joined = "";
                                  $uposts = "";
                                  $unposts = "";
                              }
                              $ip = "";
                              if (isset($_SESSION['signed_in']) | $_SESSION['user_level'] == 1) {
                                  $ip = '(' . $list['poster_ip'] . ')';
                                  if ($_SESSION['signed_in'] == false | $_SESSION['user_level'] != 1)
                                      $ip = '';
                              }
                              $last_poster = "<a href = 'user.php?u=" . $list['user_id'] . "'>" . $list['user_name'] . "</a>";
                              if ($list['user_id'] == 1) {
                                  $anonymus_color = "</b><span style='color:#AAAAAA'><i>";
                                  $end_color = "</i></span>";
                                  $last_poster = "</b><span style='color:#AAAAAA'><i>" . $list['user_name'] . "</i></span>";
                              } else {
                                  
                                  $anonymus_color = "";
                                  $end_color = "";
                              }
                              
                              $editby = "";
                              
                              if ($show_last_edited_by == 1) {
                                  if ($list['post_edit_by'] != "") {
                                      $editby = "<br />$l_last_edit " . date($date_format, strtotime($list['post_edit_date'])) . " $l_by <b>" . $list['post_edit_by'] . "</b>";
                                  }
                              }
                              
                              if (date("d.m.Y", strtotime($list['post_edit_date'])) == $date->format("d.m.Y")) {
                                  $editby = "<br />$l_last_edit <b><i>$l_today</b></i> $l_at " . date("H:i:s", strtotime($list['post_edit_date'])) . " $l_by " . $list['post_edit_by'] . "";
                              }
							  
							  if (date("d.m.Y", strtotime($list['post_edit_date'])) == date("d.m.Y",$yesterday)) {
                                  $editby = "<br />$l_last_edit <b><i>$l_yesterday</b></i> $l_at " . date("H:i:s", strtotime($list['post_edit_date'])) . " $l_by " . $list['post_edit_by'] . "";
                              }
                              
                              if ($allow_avatars == '1') {
                                  $avatar = "<img width='" . $avatar_width . "px' height='" . $avatar_height . "px' src='./img/avatars/" . $list['user_avatar'] . "'><br/>";
                                  if ($list['user_avatar'] == "" or $list['user_avatar'] == "0") {
                                      $avatar = "";
                                  }
                              } else {
                                  $avatar = "";
                              }
                              $tags = array('{GROUP}', '{POSTS}', '{U_POSTS}', '{REGISTRED}', '{D_REGISTRED}', '{P_LINK}', '{AUTHOR}', '{USERNAME}', '{POSTED}', '{POST_DATE}', '{TEXT}', '{P_ID}', '{A_COLOR}', '(END_A_COLOR)', '{IP}', '{EDITBY}', '{AVATAR}', '{TOPIC_ICON}', '{ONLINE}', '{SIGNATURE}','{GENDER}');
                              
                              if ($row['topic_icon'] != "" and $temp_numb == 1) {
                                  $icon = "<img src='./img/misc/" . $row['topic_icon'] . "' align='absmiddle'>";
                                  
                                  if ($row['topic_icon'] == "" or $row['topic_icon'] == "0") {
                                      $icon = "";
                                  }
                                  $t_icon = " $l_topic_subject $icon <b>" . $row['topic_subject'] . "</b>";
                              } else {
                                  if ($row['topic_icon'] == "" and $temp_numb == 1) {
                                      $t_icon = "$l_topic_subject $icon <b>" . $row['topic_subject'] . "</b>";
                                  }
                              }
                              if ($temp_numb != 1) {
                                  $t_icon = "RE: $icon <b>" . $row['topic_subject'] . "</b>";
                              }
                              
                              if ($user_status_posts == 1 and $users_online == 1) {
                                  $online = $db->query("SELECT user_id FROM " . $table_prefix . "online_users WHERE user_id = " . $list['user_id'] . " LIMIT 1");
                                  if (mysql_num_rows($online) > 0) {
                                      $u_online = "Status: <img src='./style/" . $default_style . "/img/online.gif' title='$l_online' align='absmiddle'/><br/>";
                                  } else {
                                      $u_online = "$l_status <img src='./style/" . $default_style . "/img/offline.gif' title='$l_offline' align='absmiddle'/><br/>";
                                  }
                                  if ($list['user_id'] == 1) {
                                      $u_online = "";
                                  }
                              }
                              
                              if ($allow_user_signature == 1) {
                                  $signature = "<br><hr size='1' noshade width='30%'>" . $list['user_signature'] . "";
                                  if ($list['user_signature'] == "") {
                                      $signature = "";
                                  }
                                  if ($images_in_signature == 0) {
                                      //$signature = preg_replace('/<font[^>]+\>/i', '', $signature);
                                      $signature = preg_replace('/<img[^>]+\>/i', '', $signature);
                                      $signature = preg_replace('/<img[^>]+\>/i', '', $signature);
                                  }
                                  
                                  if ($links_in_signature == 0) {
                                      $signature = preg_replace('/<a href[^>]+\>/i', '', $signature);
                                  }
                              }
                              if (!isset($_SESSION['signed_in']) AND $guest_show_links == 0) {
                                  $ppage = $page;
                                  if ($ppage <= 0) {
                                      $ppage = 1;
                                  }
                                  $text = preg_replace('#<a.*?>([^<]*)</a>#i', '<span style=\'color:red\'>' . $l_please_s . ' <a style="color:#CE0404;" href="signin.php?t=' . mysql_real_escape_string($_GET['t']) . '&page=' . $ppage . '">' . $l_login_s . '</a> ' . $l_or . ' <a style="color:#CE0404;" href="signup.php">' . $l_register_s . '</a> ' . $l_to_view . '</span>', $text);
                              }
                              if (isset($_GET['print'])) {
                                  echo "<hr>";
                              }
							  if ($list['is_banned'] == 1) {
							  $group = "<span style = 'color:#AAAAAA'>Banned</span><br>"; 
							  $avatar = "";
							  $signature ="";
							  $last_poster = "<a href = 'user.php?u=" . $list['user_id'] . "'><s>" . $list['user_name'] . "</s></a>";
							  }
                              //We already have .css for page printing. Now lets remove formating and links
                              if (isset($_GET['print'])) {
                                  $group = preg_replace('/<span[^>]+\>/i', '', $group);
                                  $avatar = preg_replace('/<img[^>]+\>/i', '', $avatar);
                                  $text = preg_replace('/<img[^>]+\>/i', '', $text);
                                  $text = preg_replace('/<span[^>]+\>/i', '', $text);
                                  $text = preg_replace('/<a href[^>]+\>/i', '', $text);
								  $signature = ""; 
                              }
                              $signature = str_replace("\n", "<br>", $signature);
                              $signature = smilies($signature, 'img/smilies/');	
							  //CHECK IF THIS POST NEED TO BE APPROVED
							  $approve_button =  "";
                              if ($list['post_approve'] == 1 AND 
							  $_SESSION['signed_in'] AND 
							  $_SESSION['user_level']>=1)
							  {$text.="<br><br><span style='color:red;'><b>$l_post_is_NOT_approved</b></span>";
							  $approve_button = "<a class='subforums' href = 'topic.php?t=" . mysql_real_escape_string($_GET['t']) . "&page=" . mysql_real_escape_string($_GET['page']) . "&approve=$list[post_id]'><b>$l_approve</b></a> | ";
							  } 
							  
							  if ($list['post_approve'] == 1 AND 
							  $_SESSION['signed_in']==FALSE AND 
							  $_SESSION['user_level']<1)
							  {$text=""; $hide_post =1; $totalpages = ceil(($numrows-5) / $rowsperpage);
							  } 
							  
							  if ($list['post_approve'] == 1 AND 
							  $_SESSION['signed_in']==TRUE AND 
							  $_SESSION['user_level']<1 AND $_SESSION['user_id'] == $list['post_by'])
							  {$text.="<br><br><span style='color:red;background:yellow;'><b>Your post is not approved yet!</b></span>"; $hide_post =0; $totalpages = ceil(($numrows-5) / $rowsperpage);
							  }
							  //POST DATE
							  $posted = date($date_format, strtotime($list['post_date']));
							  if (date("d.m.Y", strtotime($list['post_date'])) == $date->format("d.m.Y")) 
							  {
							  $posted = "<b>$l_today</b>, " . date("H:i:s", strtotime($list['post_date']));
							  }
							  
							  if (date("d.m.Y", strtotime($list['post_date'])) == date("d.m.Y",$yesterday)) {
                  $posted = "<b>$l_yesterday</b>, " . date("H:i", strtotime($list['post_date'])) . "";
                   }  
							  
                              $data = array($group, $unposts, $uposts, $joined, $djoined, $temp_numb, $l_sauthor, $last_poster, $l_posted, $posted, $text, $list['post_id'], $anonymus_color, $end_color, $ip, $editby, $avatar, $t_icon, $u_online, $signature,$gender_image );
                              if ($hide_post==0)
                              {echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/posts_row.html"));}
                              
                              //POST TOOLS
                              $start_post_tools = '<!--';
                              $end_post_tools = '-->';
                              $edit_post = '';
                              $delete_post = '';
                              $edit_button = '';
                              $delete_button = '';
                              $sep1 = '';
                              $sep2 = '';
                              $quote = $l_quote;
                              //IF not logged $list['post_id']
                              if (!isset($_SESSION['signed_in'])) {
                                  $start_post_tools = '<!--';
                                  $end_post_tools = '-->';
                                  $edit_post = '';
                                  $delete_post = '';
                                  $edit_button = '';
                                  $delete_button = '';
                              }
                              $sep1 = '';
                              $sep2 = '';
                              //IF allowed editing posts
                              if (isset($_SESSION['signed_in']) and $_SESSION['user_id'] == $list['user_id'] and $_SESSION['user_level'] != 1 and $allow_edit_post == 1 and $allow_delete_post == 1) {
                                  $start_post_tools = '';
                                  $end_post_tools = '';
                                  $edit_post = 'edit.php?t={T_ID}&p={POST_ID}&page={PAGE}&pn={P_LINK}';
                                  $delete_post = 'javascript:confirmDelete(\'delete.php?t={T_ID}&p={POST_ID}&page={PAGE}\')';
                                  $edit_button = $l_edit;
                                  $delete_button = $l_delete;
                                  $sep1 = $l_post_tool_sep;
                                  $sep2 = $l_post_tool_sep;
                              }
                              
                              if (isset($_SESSION['signed_in']) and $_SESSION['user_id'] == $list['user_id'] and $_SESSION['user_level'] != 1 and $allow_edit_post == 1 and $allow_delete_post == 0) {
                                  $start_post_tools = '';
                                  $end_post_tools = '';
                                  $edit_post = 'edit.php?t={T_ID}&p={POST_ID}&page={PAGE}&pn={P_LINK}';
                                  $delete_post = '';
                                  $edit_button = $l_edit;
                                  $delete_button = '';
                                  $sep1 = $l_post_tool_sep;
                                  $sep2 = '';
                              }
                              if (isset($_SESSION['signed_in']) and $_SESSION['user_id'] == $list['user_id'] and $_SESSION['user_level'] != 1 and $allow_edit_post == 0 and $allow_delete_post == 1) {
                                  $start_post_tools = '';
                                  $end_post_tools = '';
                                  $edit_post = '';
                                  $delete_post = 'javascript:confirmDelete(\'delete.php?t={T_ID}&p={POST_ID}&page={PAGE}\')';
                                  $edit_button = '';
                                  $delete_button = $l_delete;
                                  $sep1 = '';
                                  $sep2 = $l_post_tool_sep;
                              }
                              
                              //IF admin
                              if (isset($_SESSION['signed_in']) and $_SESSION['user_level'] >= 1) {
                                  $start_post_tools = '';
                                  $end_post_tools = '';
                                  $edit_post = 'edit.php?t={T_ID}&p={POST_ID}&page={PAGE}&pn={P_LINK}';
                                  $delete_post = 'javascript:confirmDelete(\'delete.php?t={T_ID}&p={POST_ID}&page={PAGE}\')';
                                  $edit_button = $l_edit;
                                  $delete_button = $l_delete;
                                  $sep1 = $l_post_tool_sep;
                                  $sep2 = $l_post_tool_sep;
                              }
                              if ($row['topic_status'] == 1 and $_SESSION['user_level'] < 1) {
                                  $start_post_tools = '<!--';
                                  $end_post_tools = '-->';
                                  $edit_post = '';
                                  $delete_post = '';
                                  $edit_button = '';
                                  $delete_button = '';
                                  $sep1 = '';
                                  $sep2 = '';
                                  $quote = "";
                              }
                              
                              if ($row['topic_status'] == 1 and $_SESSION['user_level'] < 1 and $_SESSION['signed_in'] == false) {
                                  $start_post_tools = '<!--';
                                  $end_post_tools = '-->';
                                  $edit_post = '';
                                  $delete_post = '';
                                  $edit_button = '';
                                  $delete_button = '';
                                  $sep1 = '';
                                  $sep2 = '';
                                  $quote = "";
                              }
                              $to_time = strtotime(date($date_format));
                              $from_time = strtotime($list['post_date']);
                              
                              $edit_time = ($to_time - $from_time) / 60;
                              
                              if ($edit_time >= $edit_post_time and $_SESSION['user_level'] < 1 and isset($_SESSION['signed_in'])) {
                                  $edit_post = '';
                                  $delete_post = '';
                                  $edit_button = '';
                                  $delete_button = '';
                                  $sep1 = '';
                                  $sep2 = '';
                              }
                              
                              if ($post_report == 1 and isset($_SESSION['signed_in'])) {
                                  $report_post = '<p class="alignright"><a href="javascript:confirmReport(\'topic.php?t=' . mysql_real_escape_string($_GET['t']) . '&report=' . $list['post_id'] . '\')"><img src="./style/' . $default_style . '/img/icon_post_report.gif" border=0 alt="" align="top" title="Report this post!" /></a></p>';
                              } else {
                                  $report_post = "";
                              }
							  $user_www ="";
							  $goto_profile = "<a title='$list[user_name] $l_profile' href= 'user.php?u=$list[user_id]'><img align='absmiddle' src='./style/$default_style/img/profile.gif' border=0></a>";
							  $user_www = "<a title='$list[user_www]' href= '$list[user_www]' target='_blank'><img align='absmiddle' src='./style/$default_style/img/url.png' border=0></a>";
							  
							  if ($list['user_www']=="")
							  {$user_www ="";}
							  
                              $tags = array('{START_POST_TOOLS}', '{END}', '{EDIT_POST}', '{DELETE_POST}', '{G_TOP}', '{F_ID}', '{T_ID}', '{POST_ID}', '{PAGE}', '{EDIT}', '{DELETE}', '{QUOTE}', '{QUOTE_SEL}', '{P_LINK}', '{U_ID}', '{LAST_PAGE}', '{SEPARATOR1}', '{SEPARATOR2}', '{POST_REPORT}','{APPROVE}','{PROFILE}','{WWW}');
                              $data = array($start_post_tools, $end_post_tools, $edit_post, $delete_post, $l_top, $fid, mysql_real_escape_string($_GET['t']), $list['post_id'], $pages, $edit_button, $delete_button, $quote, $l_quote_sel, $temp_numb, $list['user_name'], $current_page, $sep1, $sep2, $report_post,$approve_button,$goto_profile,$user_www);
                              
                              ////////////////////
                              if (!isset($_GET['print'])) {
                                  echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/post_tools.html"));
                              }
                              //END POST TOOLS
                              
                              //ADSENSE in posts
                              if ($adsense_in_posts == 1 and isset($_GET['print']) and $_GET['print'] != "print") {
                                  $ads = $post_per_page * $current_page;
                                  if ($temp_numb == $ads or $temp_numb == $numrows and $adsense_in_posts == 1) {
                                      $tags = array('{ADS_NAME}', '{POSTED}');
                                      $data = array($adsense_name, '' . $l_posted . ' ');
                                      echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/adsense.html"));
                                      //ADSENSE in posts
                                  }
                              }
                              echo '</table>';
                              $counter += 1;
                              $temp_c += 1;
                              if ($counter <= $post_per_page) {
                                  echo "<br>";
                              }
                          }
                          //////////////////////////////// ADMIN POST TOOLS //////////////
						  $cur_page = "&page=".$page;
						  if ($page == "")
						  {$cur_page="";}
                          if ($row['topic_sort'] == -1) {
                              $sticky = "$l_change_normal";
                              $sticky_var = 0;
                          }
                          if ($row['topic_sort'] == 0) {
                              $sticky = "$l_change_sticky";
                              $sticky_var = 1;
                          }
                          
                          if ($row['topic_status'] == 1) {
                              echo "

<img src='./style/" . $default_style . "/img/topic_locked.gif'><b>$l_error_tlocked</b><hr><br />";
                              if ($_SESSION['user_level'] > 0) {
                                  echo "<table><tr><td class='headerBar'><div align='left'>
$l_quick_tools <br><br> <a class='menuButtons' href = 'topic.php?t=" . mysql_real_escape_string($_GET['t']) . "$cur_page&sticky=$sticky_var'><b>$sticky</b></a>
 <a class='menuButtons' href = 'topic.php?t=" . mysql_real_escape_string($_GET['t']) . "$cur_page&locked=0'><b>$l_t_unlock</b></a>
 
 <a class='menuButtons' href = 'move.php?move=$row[topic_cat]&t=" . mysql_real_escape_string($_GET['t']) . "'><b>$l_t_move</b></a>
 
 <a class='menuButtons' href = 'javascript:confirmDelete(\"delete_topic.php?t=" . mysql_real_escape_string($_GET['t']) . "\")'><b>$l_delete</b></a>
</div></td></tr></table><br/>";
                              }
                          }
                          
                          if (isset($_SESSION['signed_in']) and $_SESSION['user_level'] > 0) {
                              if ($row['topic_status'] < 1) {
                                  if ($_SESSION['user_level'] > 0) {
                                      {
                                          echo "<table><tr><td class='headerBar'><div align='left'>
$l_quick_tools <br><br><a class='menuButtons' href = 'topic.php?t=" . mysql_real_escape_string($_GET['t']) . "$cur_page&sticky=$sticky_var'><b>$sticky</b></a>
 <a class='menuButtons' href = 'topic.php?t=" . mysql_real_escape_string($_GET['t']) . "$cur_page&locked=1'><b>$l_t_lock</b></a>

 <a class='menuButtons' href = 'move.php?move=$row[topic_cat]&t=" . mysql_real_escape_string($_GET['t']) . "'><b>$l_t_move</b></a>
 
 ";
                                          echo " <a class='menuButtons' href = 'javascript:confirmDelete(\"delete_topic.php?t=" . mysql_real_escape_string($_GET['t']) . "\")'><b>$l_delete</b></a>
</div></td></tr></table><br/>";
                                      }
                                  }
                              }
                          }
                          //BREADCRUMBS
                          echo "<table><tr>";
                          if ($breadcrumbs == '1') {
                              if (mysql_real_escape_string($_GET['t']) != "") {
                                  $qqq += 1;
                                  $tags = array('{FORUM}', '{SEPARATOR}', '{F_ID}', '{CAT_NAME}', '{SEPARATOR2}', '{T_SUBJECT}', );
                                  $data = array('Forum', $l_breadcrumb_separator, $row['topic_cat'], $bcrow['cat_name'], $l_breadcrumb_separator, $row['topic_subject']);
                                  if (!isset($_GET['print'])) {
                                      echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/breadcrumbs.html"));
                                  }
                              }
                          }
                          //BREADCRUMBS
                          
                          // BOTTOM PAGINATION
                          $range = $max_pagination_link;
                          if ($range >= $totalpages) {
                              $range = $totalpages;
                          }
                          echo '<td align="right" class="pagination"> <b>' . $l_page . ' ' . $current_page . ' ' . $l_page_of . ' ' . $totalpages . '</b> [' . $numrows . ' posts]  &nbsp; &nbsp;';
                          if ($currentpage > 1) {
                              echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=1'><<</a> ";
                              $prevpage = $currentpage - 1;
                              echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$prevpage'><</a> ";
                          }
                          for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
                              if (($x > 0) && ($x <= $totalpages)) {
                                  if ($x == $currentpage) {
                                      echo " [<b>$x</b>] ";
                                  } else {
                                      echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$x'>$x</a> ";
                                  }
                              }
                          }
                          if ($currentpage != $totalpages) {
                              $nextpage = $currentpage + 1;
                              echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$nextpage'>></a> ";
                              echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$totalpages'>>></a> ";
                          }
                          echo '</td></tr></table> ';
                          // BOTTOM PAGINATION END
                          if ($row['topic_status'] != 1) {
                              if (!isset($_SESSION['signed_in']) and $allow_guest_posts == "0") {
                                  echo '</tr></td></table><br /><div align="center">' . $l_you_need_to . ' <a class="loginlink" href="signin.php?t=' . mysql_real_escape_string($_GET['t']) . '&page=' . $current_page . '">' . $l_login_s . '</a> ' . $l_to_reply . ' <a class="loginlink" href="signup.php">' . $l_register_s . '</a> ' . $l_for_acc . '</div>';
                              } else {
                                  if (!$_SESSION['signed_in'] and $allow_guest_posts == "1") {
                                      $_SESSION['user_id'] = 1;
                                      echo '</tr></td></table><div align="center">' . $l_before_post . ' <a class="loginlink" href="signin.php?t=' . mysql_real_escape_string($_GET['t']) . '&page=' . mysql_real_escape_string($_GET['page']) . '">' . $l_login_s . '</a> ' . $l_to_reply_username . ' <a class="loginlink" href="signup.php">' . $l_register_s . '</a> ' . $l_for_acc . '</div>';
                                  }
                              }
                              //finish tde table
                              echo '</table>';
                              //////////////////////////////// END ADMIN POST TOOLS //////////////
                              $if_smilies = "";
                              $endif_smilies = "";
                              $if_guest_post = "";
                              $endif_guest_post = "";
                              
                              if ($allow_smilies == 0) {
                                  $if_smilies = "<!--";
                                  $endif_smilies = "-->";
                                  $if_guest_post = "<!--";
                                  $endif_guest_post = "-->";
                              }
                              
                              if ($allow_smilies == 0 and $allow_guest_posts == "1") {
                                  $if_smilies = "<!--";
                                  $endif_smilies = "-->";
                                  $if_guest_post = "";
                                  $endif_guest_post = "";
                              }
                              
                              if ($allow_smilies == 1 and $allow_guest_posts == "1") {
                                  $if_smilies = "";
                                  $endif_smilies = "";
                                  $if_guest_post = "";
                                  $endif_guest_post = "";
                              }
                              
                              if ($allow_smilies == 1 and $allow_guest_posts == "0") {
                                  $if_smilies = "";
                                  $endif_smilies = "";
                                  $if_guest_post = "<!--";
                                  $endif_guest_post = "-->";
                              }
                              
                              if ($allow_smilies == 1 and $allow_guest_posts == "1" and $_SESSION['user_id'] > 1) {
                                  $if_smilies = "";
                                  $endif_smilies = "";
                                  $if_guest_post = "<!--";
                                  $endif_guest_post = "-->";
                              }
                              
                              if ($captcha_for_guests == "0") {
                                  $if_guest_post = "<!--";
                                  $endif_guest_post = "-->";
                              }
                              if ($captcha_for_guests == "1" and isset($_SESSION['signed_in'])) {
                                  $if_guest_post = "<!--";
                                  $endif_guest_post = "-->";
                              }
                              
                              if ($default_wysiwyg == "1") {
                                  $default_editor = "";
                              }
                              if ($default_wysiwyg != "1") {
                                  $default_editor = "initEditor(\"reply\", true);  SwitchEditor()";
                              }
                              
                              $editor_css = "./style/$default_style/editor.css";
                              $editor = "./style/$default_style/editor.js";
                              $current_page = $totalpages;
                              $tags = array('{REPLY}', '{MORE_SMILIES}', '{DISABLE_SMILIES}', '{F_ID}', '{T_ID}', '(PAGE)', '{CREATE}', '{QUOTE_SEL}', '{IF_SMILIES}', '{END_SMILIES}', '(SUBMIT_REPLY)', '{IFCAPTCHA}', '{END}', '{EDITOR_CSS}', '{EDITOR}', '{WYSIWYG}','{PREVIEW}','{DISABLE_BB}');
                              $data = array($l_reply_, $l_more_smilies, $l_dissmilies, $fid, mysql_real_escape_string($_GET['t']), $current_page, $l_create_topic, $l_quote_sel, $if_smilies, $endif_smilies, $l_submit_reply, $if_guest_post, $endif_guest_post, $editor_css, $editor, $default_editor,$l_preview_button,$l_disable_bb);
                              
                              $show_postform = '1';
                              if ($allow_guest_posts == 1 and $_SESSION['signed_in'] == false) {
                                  $show_postform = '1';
                              }
                              if ($allow_guest_posts == 0 and !isset($_SESSION['signed_in'])) {
                                  $show_postform = '0';
                              }
                              
                              if (isset($_SESSION['signed_in'])) {
                                  $show_postform = '1';
                              }
                              
                              if (!isset($_GET['print'])) {
                                  if ($show_postform == 1) {
                                      echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/post_form.html"));
                                  }
                              } //END print page
                          } // END checking topic status
                          if (isset($_SESSION['signed_in'])) {
                              if ($allow_edit_post == 1) {
                                  echo '' . $l_allowed_toedit . '<br />';
                              }
                              if ($allow_delete_post == 1) {
                                  echo $l_allowed_todelete;
                              } //END IF allow_delete_post
                          } //END IF signed_in
						  
                      } //END IF hide_forum!=1 (forum permission)
                  } //END IF topic_id != _get't'
              } //END IF this topic exists
          } //END IF no query error
      } //END IF checking guest access
  } //END IF _get't' not empty
  if ($hide_footer < 1) {
      include 'footer.php';
  }
  $pageContents = ob_get_contents();
  ob_end_clean();
  echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
?>